package com.wyu.notaryserver.controller;


import org.json.JSONObject;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
/**
 * @author 六味lemontea 2024-12-29
 * @version 1.0
 * @description
 */
public class ProcessController {
    public static String Cookie;

    public static void main(String[] args) {
        Cookie = getCookie();
        GenerateDocumentUtil.generateDocument();
    }
    static String getCookie() {
        try {
            // 创建HttpClient
            HttpClient client = HttpClient.newHttpClient();

            // 构建GET请求
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create("https://test.wygzc.cn/v1/api/ai/get/notary/cookie"))
                    .header("Accept", "application/json")
                    .GET()
                    .build();
            // 发送请求并获取响应
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

            // 解析响应体JSON并提取msg字段
            JSONObject jsonObject = new JSONObject(response.body());
            return jsonObject.optString("msg", "");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static class GenerateDocumentUtil {
        public static void generateDocument()  {
            // 创建 HTTP Client
            HttpClient client = HttpClient.newHttpClient();
            String jh = saveSLdjRequest(client);
            autoCreateFileRequest(client,jh);
            saveSqRequest(client);
        }

        public static String saveSLdjRequest(HttpClient client) {

            // 构建 HTTP 请求体
            String url = "https://gzgl.gdsf.gov.cn/gzywxt/ywbl/sl/saveSLdj";
            String requestBody = "gzRyData=%5B%7B%22zz%22%3A%22%E5%B9%BF%E4%B8%9C%E7%9C%81%E6%B1%9F%E9%97%A8%E5%B8%82%E8%93%AE%E6%B1%9F%E5%8C%BA%E4%B8%B0%E7%9B%9B%E8%8B%9123%E5%B9%A21701%E5%AE%A4%22%2C%22csrq%22%3A%222002-07-12%22%2C%22wbm%22%3Anull%2C%22yxqxKs%22%3Anull%2C%22zjlx%22%3A%222011110903%22%2C%22pym%22%3Anull%2C%22frdbdydw%22%3A%22-1%22%2C%22ssmk%22%3A%22SL%22%2C%22px%22%3A1%2C%22tbbs%22%3A0%2C%22sjly%22%3Anull%2C%22czr%22%3A%22106418%22%2C%22bzsl%22%3A1%2C%22czsj%22%3A%222024-12-13T15%3A52%3A07%22%2C%22lxdh%22%3A%2216626592158%22%2C%22gj%22%3A%22156%22%2C%22localTx%22%3Anull%2C%22yxqxJs%22%3Anull%2C%22rylx%22%3A%22DLR%22%2C%22czbs%22%3A0%2C%22qfjg%22%3Anull%2C%22xb%22%3A%222011110802%22%2C%22mz%22%3Anull%2C%22xm%22%3A%22%E9%98%AE%E6%9C%9D%E9%94%A6%22%2C%22dqbm%22%3A%22440703%22%2C%22rybh%22%3A%2220240409160445711985%22%2C%22zjh%22%3A%22440882200207124737%22%2C%22jgbm%22%3A%2232440000007066547D%22%2C%22jh%22%3A%22WYGZC2022Z18325%22%2C%22sflzr%22%3A1%7D%5D&sxData=%5B%7B%22sxmc%22%3A%22%E7%AC%A6%E5%90%88%E8%A7%84%E5%AE%9A%E6%9D%A1%E4%BB%B6%E7%9A%84%E5%85%B6%E4%BB%96%E5%80%BA%E6%9D%83%E6%96%87%E4%B9%A6%22%2C%22fbs%22%3A%221%22%2C%22bde%22%3A%22%22%2C%22sxbz%22%3A%22%22%2C%22yw%22%3A%22%22%2C%22ywText%22%3A%22%22%2C%22sxFlag%22%3A%22dataTable_014014_0_0%22%2C%22sx%22%3A%22dataTable_014014_0_0_0%22%2C%22slbh%22%3A%22%22%2C%22zh%22%3A%22%22%2C%22sxIndexFlag%22%3A%22014014_0_index0%22%2C%22pldjsl%22%3A%221%22%2C%22pxnumber%22%3A%221%22%2C%22sqrs%22%3A%22%E9%98%AE%E6%9C%9D%E9%94%A6%22%2C%22gzFyList%22%3A%5B%7B%22tempId%22%3A1734595692883%2C%22sfxm%22%3A%222011111602%22%2C%22sfxmmc%22%3A%22%E5%85%AC%E8%AF%81%E8%B4%B9%22%2C%22ysje%22%3A0%2C%22ssje%22%3A0%2C%22je%22%3A0%2C%22zjf%22%3A0%2C%22jmf%22%3A0%2C%22tf%22%3A0%2C%22LAY_TABLE_INDEX%22%3A0%7D%2C%7B%22tempId%22%3A1734595695235%2C%22sfxm%22%3A%222011111605%22%2C%22sfxmmc%22%3A%22%E5%89%AF%E6%9C%AC%E8%B4%B9%22%2C%22ysje%22%3A22%2C%22ssje%22%3A0%2C%22je%22%3A%2222%22%2C%22zjf%22%3A0%2C%22jmf%22%3A0%2C%22tf%22%3A0%2C%22LAY_TABLE_INDEX%22%3A1%7D%5D%7D%5D&sljbData=%7B%22bm%22%3A%2220220208092916534472%22%2C%22gzy%22%3A%22100795%22%2C%22gzyText%22%3A%22%E8%83%A1%E6%B5%A9%E5%88%9D%22%2C%22xbr%22%3A%22106418%22%2C%22xbrText%22%3A%22%E9%98%AE%E6%9C%9D%E9%94%A6%22%2C%22zl%22%3A%22101391%22%2C%22zlText%22%3A%22%E4%BD%99%E5%98%89%E4%B8%9A%22%2C%22zxzl%22%3A%22101391%22%2C%22zxzlText%22%3A%22%E4%BD%99%E5%98%89%E4%B8%9A%22%2C%22lb%22%3A%2220120714183932734008%22%2C%22syd%22%3A%222011111202%22%2C%22yt%22%3A%2220180423142028316005%22%2C%22jj%22%3A%221%22%2C%22slsj%22%3A%222024-12-19+16%3A04%3A58%22%2C%22sfwgyflfw%22%3A%221%22%2C%22sfwgyflfwText%22%3A%22%E9%9D%9E%E5%85%AC%E7%9B%8A%E6%B3%95%E5%BE%8B%E6%9C%8D%E5%8A%A1%E7%B1%BB%E5%85%AC%E8%AF%81%22%2C%22sfsyqzqgz%22%3A%221%22%2C%22sfsyqzqgzText%22%3A%22%E6%97%A0%22%2C%22rz%22%3Afalse%2C%22fbjs%22%3Afalse%2C%22yd%22%3Afalse%2C%22sffy%22%3Afalse%2C%22sfzscq%22%3Afalse%2C%22fwlx%22%3A%5B%7B%22name%22%3A%22%E6%97%A0%22%2C%22value%22%3A1%2C%22selected%22%3Atrue%7D%5D%2C%22qsz%22%3Afalse%2C%22ajbq%22%3A%2220220519101849224877%22%2C%22qtbq%22%3A%2220221206172932562764%22%2C%22cljg%22%3Anull%2C%22sbbh%22%3Anull%2C%22lzdd%22%3A%22%22%2C%22yjdz%22%3A%22%22%2C%22yjdh%22%3A%22%22%2C%22yjsjr%22%3A%22%22%2C%22yjlxdh%22%3A%22%22%2C%22dzyx%22%3A%22%22%2C%22bz%22%3A%22%22%2C%22fyr%22%3A%22%22%2C%22nczrq%22%3A%222025-01-08%22%7D&fromFlag=&jhMr=-1&uuid=2024121916045028461647691b98af33&sffzws=";

            // 创建 HTTP 请求
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .header("Accept", "*/*")
                    .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,pt-PT;q=0.5,pt;q=0.4")
                    .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                    .header("Cookie", "supervisor_shiro_redis_session=" +Cookie)
                    .header("Origin", "https://gzgl.gdsf.gov.cn")
                    .header("Referer", "https://gzgl.gdsf.gov.cn/gzywxt/ywbl/sl/toAdd?resourceId=20210220152946688326885feb993c35")
                    .header("X-Requested-With", "XMLHttpRequest")
                    .header("sec-ch-ua", "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0")
                    .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                    .build();

            try {
                // 发送请求并获取响应
                HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

                // 打印响应状态和内容
                System.out.println("Response Status: " + response.statusCode());
                System.out.println("Response Body: " + response.body());
                JSONObject jsonObject = new JSONObject(response.body());

                // 获取 "data" 对象
                JSONObject data = jsonObject.getJSONObject("data");

                // 提取 "jh" 字段的值
                return data.getString("jh");
            } catch (Exception e) {
                e.printStackTrace();
            }
//            throw new RuntimeException("没有正确返回jh字段");
            return "";
        }

        public static void autoCreateFileRequest(HttpClient client,String jh){

            try {

                // 构造请求体
                String requestBody = "jh="+jh;

                // 创建HttpRequest
                HttpRequest request = HttpRequest.newBuilder()
                        .uri(new URI("https://gzgl.gdsf.gov.cn/gzywxt/ywbl/sl/autoCreateFile"))
                        .header("Accept", "*/*")
                        .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,pt-PT;q=0.5,pt;q=0.4")
                        .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                        .header("Cookie", "supervisor_shiro_redis_session="+Cookie)
                        .header("Origin", "https://gzgl.gdsf.gov.cn")
                        .header("Referer", "https://gzgl.gdsf.gov.cn/gzywxt/ywbl/sl/toAdd?resourceId=20210220152946688326885feb993c35")
                        .header("Sec-Fetch-Dest", "empty")
                        .header("Sec-Fetch-Mode", "cors")
                        .header("Sec-Fetch-Site", "same-origin")
                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0")
                        .header("X-Requested-With", "XMLHttpRequest")
                        .header("sec-ch-ua", "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"")
                        .header("sec-ch-ua-mobile", "?0")
                        .header("sec-ch-ua-platform", "\"Windows\"")
                        .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                        .build();

                // 发送请求并接收响应
                HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

                // 打印响应状态和内容
                System.out.println("Response Code: " + response.statusCode());
                System.out.println("Response Body: " + response.body());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void saveSqRequest(HttpClient client){
            try {
                // 请求体数据
                String requestBody = "dsrDataJson=%5B%7B%22xm%22%3A%22%E9%98%AE%E6%9C%9D%E9%94%A6%22%2C%22xb%22%3A%222011110802%22%2C%22mz%22%3Anull%2C%22zj%22%3A%222011110903%22%2C%22zh%22%3A%22440882200207124737%22%2C%22cs%22%3A%222002-07-12%22%2C%22dh%22%3A%2216626592158%22%2C%22dz%22%3A%22%E5%B9%BF%E4%B8%9C%E7%9C%81%E6%B1%9F%E9%97%A8%E5%B8%82%E8%93%AE%E6%B1%9F%E5%8C%BA%E4%B8%B0%E7%9B%9B%E8%8B%9123%E5%B9%A21701%E5%AE%A4%22%2C%22photoArray%22%3A%22%22%2C%22index%22%3A0%7D%5D&sxDataJson=%5B%7B%22sx%22%3A%22014014%22%2C%22sxbz%22%3A%22%22%2C%22ry%22%3A%22%22%2C%22gzryArr%22%3A%5B%7B%22index%22%3A0%2C%22value%22%3A%22%E9%98%AE%E6%9C%9D%E9%94%A6%22%7D%5D%2C%22xgrArr%22%3A%5B%5D%7D%5D&clDataJson=%5B%5D&bm=20220208092916534472&gzr=100795&xbr=106418&lb=20120714183932734008&yt=20180423142028316005&syd=2011111202&rz=1&jij=1&jjbz=&tbbs=1";

                // 创建HttpRequest
                HttpRequest request = HttpRequest.newBuilder()
                        .uri(new URI("https://gzgl.gdsf.gov.cn/gzywxt/ywbl/sq/saveSq"))
                        .header("Accept", "*/*")
                        .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,pt-PT;q=0.5,pt;q=0.4")
                        .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                        .header("Cookie", "supervisor_shiro_redis_session="+Cookie)
                        .header("Origin", "https://gzgl.gdsf.gov.cn")
                        .header("Referer", "https://gzgl.gdsf.gov.cn/gzywxt/ywbl/sl/toAdd?resourceId=20210220152946688326885feb993c35")
                        .header("Sec-Fetch-Dest", "empty")
                        .header("Sec-Fetch-Mode", "cors")
                        .header("Sec-Fetch-Site", "same-origin")
                        .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0")
                        .header("X-Requested-With", "XMLHttpRequest")
                        .header("sec-ch-ua", "\"Microsoft Edge\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"")
                        .header("sec-ch-ua-mobile", "?0")
                        .header("sec-ch-ua-platform", "\"Windows\"")
                        .POST(HttpRequest.BodyPublishers.ofString(requestBody))
                        .build();

                // 发送请求并接收响应
                HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

                // 打印响应状态和内容
                System.out.println("Response Code: " + response.statusCode());
                System.out.println("Response Body: " + response.body());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
